package com.zhika.zishi.fragments.set;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zhika.zishi.MyApplication;
import com.zhika.zishi.R;
import com.zhika.zishi.customview.TitleView;
import com.zhika.zishi.interfaces.TitleInterface;

/**
 * Created by shi-02 on 2015/4/18.
 */
public class ModifyPasswordFragment extends Fragment {

    private TitleView titleView;

    private View view;

    private Context context;

    private EditText oldPasswordEdit;

    private EditText newPasswordEdit;

    private EditText newPasswordEdit2;

    private Button sureButton;

    public interface OnModifyPasswordInterface {

        //移除此Fragment
        public void remoteModifyPasswordFragment();

        public void sureModifyPassword(String oldPassword, String newPassword);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.modify_password_layout, container, false);

        initView();

        return view;
    }

    private void initView() {
        context = getActivity();

        titleView = (TitleView) view.findViewById(R.id.modify_password_title_view);
        titleView.setLeftButtonText("返回");
        titleView.setMiddleRowText("更改登陆密码");
        titleView.hideRightButton();
        titleView.setTitleViewInterface(new TitleInterface() {
            @Override
            public void leftTitleListener() {
                ((OnModifyPasswordInterface) context).remoteModifyPasswordFragment();
            }

            @Override
            public void middleTitleListener() {

            }

            @Override
            public void rightTitleListener() {

            }
        });

        oldPasswordEdit = (EditText) view.findViewById(R.id.modify_password_old_edit);
        newPasswordEdit = (EditText) view.findViewById(R.id.modify_password_new_edit);
        newPasswordEdit2 = (EditText) view.findViewById(R.id.modify_password_new_2_edit);

        sureButton = (Button) view.findViewById(R.id.modify_password_sure_button);
        sureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                modifyPassword();

            }
        });
    }

    /**
     * 修改密码
     */
    private void modifyPassword() {
        String msg = "";
        if (TextUtils.isEmpty(oldPasswordEdit.getText().toString())) {
            msg = "请输入旧密码";
        } else if (TextUtils.isEmpty(newPasswordEdit.getText().toString())) {
            msg = "请输入新密码";
        } else if (!newPasswordEdit2.getText().toString().equals(newPasswordEdit.getText().toString())) {
            msg = "两次输入的密码不一样";
        } else {
            ((OnModifyPasswordInterface) context).sureModifyPassword(oldPasswordEdit.getText().toString(), newPasswordEdit2.getText().toString());
        }

        if (!msg.equals("")) {
            MyApplication.showToast(msg);
        }
    }
}
